# -*- coding: utf-8 -*-  
# @File : bar图_不同方法对比.py
# @Time : 2024/6/3 18:11
# @Author : Alec
# @Func : 
# @Desc :



import matplotlib.pyplot as plt
import numpy as np

# Data
techniques = ['MLP', 'RNN-A', 'RNN-B']
training_errors = [27.1, 24.1, 17.0]
validation_errors = [27.0, 23.8, 16.5]

# Bar width
bar_width = 0.35
gap = 0.05

# Colors
training_color = '#1f77b4'  # Blue
validation_color = '#ff7f0e'  # Orange

# Positions of the bars on the x-axis
r1 = np.arange(len(techniques)) * (1 + bar_width + gap)
r2 = r1 + bar_width + gap

# Create figure and axis
fig, ax = plt.subplots()

# Create bars
bars1 = ax.bar(r1, training_errors, color=training_color, width=bar_width, edgecolor='grey', label='Training', hatch='///')
bars2 = ax.bar(r2, validation_errors, color=validation_color, width=bar_width, edgecolor='grey', label='Validation', hatch='\\\\\\')

# Add text annotations
for bar in bars1:
    yval = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2.0, yval, round(yval, 1), va='bottom', ha='center', color=training_color)  # va: vertical alignment

for bar in bars2:
    yval = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2.0, yval, round(yval, 1), va='bottom', ha='center', color=validation_color)  # va: vertical alignment

# Add labels and title
ax.set_xlabel('Predictive Modeling Technique')
ax.set_ylabel('Prediction Error (mm)')
ax.set_title('Comparison of prediction error of different network structures')
ax.set_xticks([r + bar_width/2 for r in range(len(techniques))])
ax.set_xticklabels(techniques)
ax.set_ylim([10, 30])

# Add legend
ax.legend()

# Save the plot as a high-quality image
plt.tight_layout()
# plt.savefig('./Photos/comparison_of_prediction_error_corrected.png', dpi=600)

# Show plot
plt.show()


